import React from 'react';
import {Tree} from 'antd';
import {connect} from "react-redux";
import {Form} from "antd/lib/index";
import {deepFullCopy} from "share/common";

let transTreeData = [];
let waitDealTreeData = [];
const TreeNode = Tree.TreeNode;

class TreeSelector extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      treeData: props.treeData,
      selectNodeData: []
    };
  }

  componentWillMount() {
    if (this.props.isListTransTreeData) {
      waitDealTreeData = this.props.treeData;
      this.setTreeData(this.getTopTreeFromList(waitDealTreeData));
      this.setState({
        treeData: deepFullCopy(transTreeData)
      })
    }

  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.treeData != this.props.treeData) {
      if (nextProps.isListTransTreeData) {
        waitDealTreeData = nextProps.treeData;
        transTreeData = [];
        this.setTreeData(this.getTopTreeFromList(deepFullCopy(waitDealTreeData)));
        this.setState({
          treeData: deepFullCopy(transTreeData)
        })
      } else {
        this.setState({
          treeData: nextProps.treeData
        })
      }
    }
  }

//从后端的列表数据，处理成树数据
  getTopTreeFromList(list) {
    let {parentIdAlias} = this.props;
    var arr = [];
    for (var i = 0; i < list.length; i++) {
      if (!list[i][parentIdAlias]) {
        if (this.checkIsHasChildren(list[i], list)) {
          list[i].ownChildrenNode = true
        } else {
          list[i].ownChildrenNode = false
        }
        arr.push(list[i])
      }
    }
    return arr;
  }

//判断这个部门是否有子部门，后端返回的节点没有字段标志说这个部门是否是父级部门
  checkIsHasChildren(item, list) {
    let {treeNodeIdAlias, parentIdAlias} = this.props;
    for (var i = 0; i < list.length; i++) {
      if (item[treeNodeIdAlias] + "" === list[i][parentIdAlias]) {
        return true;
      }
    }
    return false;
  }

//设置树的第一层
  setTreeData(data) {
    let {treeNodeNameAlias, treeNodeIdAlias} = this.props;
    for (var i = 0; i < data.length; i++) {
      const info = {};
      info[treeNodeNameAlias] = data[i][treeNodeNameAlias];
      info[treeNodeIdAlias] = data[i][treeNodeIdAlias];
      info.originData = data[i];
      if (data[i].ownChildrenNode) {
        info.isLeaf = false;
        var list = this.getChildrenFromTreeList(data[i][treeNodeIdAlias], waitDealTreeData);
        this.setLeafByParentOID(list, info);
      } else {
        info.isLeaf = true;
      }
      transTreeData.push(info);
    }
  }

//把子节点挂在父节点上
  setLeafByParentOID(childNodes, parentNode) {
    var children = [];
    let {treeNodeNameAlias, treeNodeIdAlias, childrenTreeAlias} = this.props;
    for (var i = 0; i < childNodes.length; i++) {
      const info = {};
      info[treeNodeNameAlias] = childNodes[i][treeNodeNameAlias];
      info[treeNodeIdAlias] = childNodes[i][treeNodeIdAlias];
      info.originData = childNodes[i];
      if (childNodes[i].ownChildrenNode) {
        info.isLeaf = false;
        var list = this.getChildrenFromTreeList(childNodes[i][treeNodeIdAlias], waitDealTreeData);
        this.setLeafByParentOID(list, info);
      } else {
        info.isLeaf = true;
      }
      children.push(info);
    }
    parentNode[childrenTreeAlias] = children;
  }

  // 获取对应子树从前端缓存的列表中
  getChildrenFromTreeList(id, list) {
    var arr = [];
    let {parentIdAlias} = this.props;
    for (var i = 0; i < list.length; i++) {
      if (list[i][parentIdAlias] + "" === id + "") {
        if (this.checkIsHasChildren(list[i], list)) {
          list[i].ownChildrenNode = true
        } else {
          list[i].ownChildrenNode = false
        }
        arr.push(list[i])
      }
    }
    return arr;
  }

  onSelect = (selectedKeys, info) => {
    /* let {selectNodeData}=this.props;
     if(info.selected){
       selectNodeData.push(selectedKeys[0])
       selectNodeData.push("2")
     }else{
       selectNodeData.splice(selectedKeys[0],1)
     }*/
  };

  onRightClick = (event, node) => {
    let {isEnableRightClick} = this.props;
    if (isEnableRightClick) {

    }
  };

  onCheck = (checkedKeys) => {
    let {isCheckStrictly, selectNodeData} = this.props;
  /*  if (!isCheckStrictly) {
      selectNodeData = deepFullCopy(checkedKeys);
    } else {
      selectNodeData = deepFullCopy(checkedKeys.checked)
    }*/
    let selectNodeDatas;
    if (!isCheckStrictly) {
      selectNodeDatas = checkedKeys;
    } else {
      selectNodeDatas = checkedKeys.checked
    }
   /* this.setState({selectNodeData});*/
    this.props.callBackSelectValue(selectNodeDatas)
  };


  render() {
    const {childrenTreeAlias, treeNodeNameAlias, treeNodeIdAlias, selectNodeData, isCheckStrictly} = this.props;
    const {treeData} = this.state;
    let mapTreeNode = function (trees) {
      return trees && trees.length > 0 && trees.map(item => {
        return (item && item[treeNodeIdAlias] && <TreeNode title={item[treeNodeNameAlias]} key={item[treeNodeIdAlias]}>
          {mapTreeNode(item[childrenTreeAlias])}
        </TreeNode>)
      })
    }
    return (
      <div>
        {(treeData && treeData.length > 0 ) ? <Tree
          showLine
          onCheck={this.onCheck}
          checkable
          multiple={true}
          checkStrictly={isCheckStrictly}
          selectedKeys={selectNodeData}
          onRightClick={this.onRightClick}>
          {mapTreeNode(treeData)}
        </Tree> : <div></div>}
      </div>
    );
  }
}

TreeSelector.propType = {
  treeData: React.PropTypes.array,//整棵树对象信息
  treeNodeIdAlias: React.PropTypes.string.isRequired,//节点树唯一编号
  treeNodeNameAlias: React.PropTypes.string.isRequired,//节点树变量别名，
  selectNodeData: React.PropTypes.array,//默认选中的节点
  childrenTreeAlias: React.PropTypes.string,//子节点树别名,clickLoadChildrenNodeData为false，此项必填
  isListTransTreeData: React.PropTypes.bool,//平铺list，根据parentID转换，树型数据。
  parentIdAlias: React.PropTypes.string,//list父节点属性名，若isListTransTreeData为true，此项必填。
  ownChildNodeAlias: React.PropTypes.string,//list是否拥有子节点属性名，若isListTransTreeData为true，此项必填。
  selectTreeNodeType: React.PropTypes.number,//选择树节点类型。1.节点树高亮 2.复选框显示
  isEnableRightClick: React.PropTypes.bool,//是否启用右击，功能：包含选择包含子节点
  isCheckStrictly: React.PropTypes.bool,//功能：包含选择包含子节点
  isEnableCustomTreeNodeInfo: React.PropTypes.bool,//是否启用自定义节点DOM
  customTreeNodeInfo: React.PropTypes.func,//自定义节点React信息,传递对象信息
  ownChildrenNodeExpressAlias: React.PropTypes.string,//是否拥有子节点树表示
  clickLoadChildrenNodeData: React.PropTypes.bool,//是否点击加载子节点信息
  getChildrenNodeDataUrl: React.PropTypes.string,//加载子节点URL
  callBackSelectValue: React.PropTypes.func,//回调选中的Value

}
TreeSelector.defaultProps = {
  selectTreeNodeType: 1,
  selectNodeData: [],
  isEnableCustomTreeNodeInfo: false,
  clickLoadChildrenNodeData: false,
  isEnableRightClick: false,
  isListTransTreeData: false,
  isCheckStrictly: true,
  callBackSelectValue: () => {
  }
}

function mapStateToProps(state) {
  return {
    authToken: state.main.authToken
  }
}

const WrappedTreeSelect = Form.create()(TreeSelector);

export default connect(mapStateToProps)(WrappedTreeSelect);
